

const tools = require('../util/tool');
module.exports = class cost_sql extends require('./mysql'){

    static getCostDataSql(c_id,paymoney,paystatus,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(currPage * pageNum);
        return new Promise((resolve , reject)=>{
            // let sql = "select * from `cost` where c_id = "+ c_id+" limit ?,?";
            let sql="select * from `cost` where 1=1";
            if (c_id) sql += " and c_id= '" + c_id + "'";
            if (paymoney) sql += " and paymoney like '%"+paymoney+"%'";
            if (paystatus) sql += " and paystatus= '" + paystatus + "'";
            sql += " limit ?,?";
            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }
    /**
     * 公告列表总数
     * @returns {Promise<any>}
     */
    static getCostDataTotal(c_id,paymoney,paystatus){
        return new Promise((resolve, reject) => {
            // let sql = "select count(1) as count from `cost` where c_id = "+c_id
            let sql='select count(1) as count from `cost` where 1=1';
            if (c_id) sql += " and c_id= '" + c_id + "'";
            if (paymoney) sql += " and paymoney like '%"+paymoney+"%'";
            if (paystatus) sql += " and paystatus= '" + paystatus + "'";
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 缴费项目新增
     * @param communityname
     * @param title
     * @param content
     * @returns {Promise<any>}
     */
    static addCostDataSql(payname,paylevel,paynum,paymoney,paystatus,c_id,paycontent,coststatus){

        return new Promise((resolve, reject) => {
            let sql = "insert into `cost`(payname,paylevel,paynum,paymoney,paystatus,c_id,paycontent,coststatus,paytime) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
            let paytime = tools.formatDate()
            // let value = [u_id,username,password,type,email,nickname,sex,address,note,createtime];
            console.log(sql)
            this.query(sql,this.formarParams(payname,paylevel,paynum,paymoney,paystatus,c_id,paycontent,coststatus,paytime)).then(result=>{
                resolve('新增成功')
            }).catch(err=>{
                reject('新增失败')
            })
        })
    }


    /**
     * 获取计费项目详情
     * @param id
     * @returns {Promise<any>}
     */
    static getCostDetailSql(id){
        return new Promise((resolve , reject)=>{
            let sql = 'select * from `cost` where id='+id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 删除计费项目
     * @param id
     * @returns {Promise<any>}
     */
    static delCostDataSql(id){
        return new Promise((resolve, reject) => {
            let sql = 'delete from `cost` where id ='+id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('删除计费信息成功')
            }).catch(err=>{
                reject('删除计费信息失败')
            })
        })
    }


    /**
     * 联表更新缴费状态
     * @param c_id
     * @param coststatus
     * @returns {Promise<any>}
     */
    static updateCostDataSql(u_id,c_id,coststatus){
        return new Promise((resolve, reject) => {
            let sql = 'update `user` set coststatus="'+coststatus+'",c_id="'+c_id+'" where id ='+u_id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }

    static upCostSql(c_id,paystatus,username){
        return new Promise((resolve, reject) => {
            let sql = 'update `cost` set paystatus="'+paystatus+'",username="'+username+'" where id ='+c_id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }


    /**
     * 支付信息列表分页获取
     * @param username
     * @param payname
     * @param paytype
     * @param coststatus
     * @param startDate
     * @param endDate
     * @param paymoney
     * @param paylevel
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getPayMessageSql(username,payname,costtype,coststatus,startDate,endDate,paymoney,paylevel,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(pageNum * currPage);
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `cost` where 1=1";
            if (username) sql += " and username like '%"+username+"%'";
            if (payname) sql += " and payname like '%"+payname+"%'";
            if (costtype) sql += " and costtype= '" + costtype + "'";
            if (coststatus) sql += " and coststatus= '" + coststatus + "'";
            if (startDate && endDate) sql += " and (costtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
            if (paymoney) sql += " and paymoney like '%"+paymoney+"%'";
            if (paylevel) sql += " and paylevel like '%"+paylevel+"%'";
            sql += " limit ?,?";
            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 支付信息总数获取
     * @param username
     * @param payname
     * @param paytype
     * @param coststatus
     * @param startDate
     * @param endDate
     * @param paymoney
     * @param paylevel
     * @returns {Promise<any>}
     */
    static getPayMessageTotal(username,payname,costtype,coststatus,startDate,endDate,paymoney,paylevel){
        return new Promise((resolve, reject)=>{
            // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
            let sql='select count(1) as count from `cost` where 1=1';
            if (username) sql += " and username like '%"+username+"%'";
            if (payname) sql += " and payname like '%"+payname+"%'";
            if (costtype) sql += " and costtype= '" + costtype + "'";
            if (coststatus) sql += " and coststatus= '" + coststatus + "'";
            if (startDate && endDate) sql += " and (costtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
            if (paymoney) sql += " and paymoney like '%"+paymoney+"%'";
            if (paylevel) sql += " and paylevel like '%"+paylevel+"%'";
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 用户支付
     * @param u_id
     * @param paystatus
     * @param costtype
     * @param costcontent
     * @returns {Promise<any>}
     */
    static updateUserPaySql(u_id,coststatus,costtype,costcontent){
        let currTime = tools.formatDate();
        return new Promise((resolve, reject) => {
            let sql = 'update `cost` set coststatus="'+coststatus+'", costtype="'+costtype+'", costcontent="'+costcontent+'", costtime="'+currTime+'" where id ='+u_id;
            console.log(sql)
            this.query(sql).then(result=>{
                resolve('支付成功')
            }).catch(err=>{
                reject('支付失败')
            })
        })
    }

    static updateUserPayStatusSql(u_id,coststatus){
        return new Promise((resolve, reject) => {
            let sql = 'update `user` set coststatus="'+coststatus+'" where c_id ='+u_id;
            console.log(sql)
            this.query(sql).then(result=>{
                resolve('支付成功')
            }).catch(err=>{
                reject('支付失败')
            })
        })
    }


}